  unit UntNovinDelphiSample;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, Mask, Menus , Clipbrd, Spin,NovinAfzarNT64_TLB,
  Buttons;

type
  TFrmNovinDelphiSample = class(TForm)
    lbVID: TLabel;
    edVID: TEdit;
    lbPassword: TLabel;
    edPassword: TEdit;
    PageControl1: TPageControl;
    TabGeneral: TTabSheet;
    TabBlockData: TTabSheet;
    TabData: TTabSheet;
    TabEncryption: TTabSheet;
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    lbChkDeviceReady: TLabel;
    lbPID: TLabel;
    lbSerial: TLabel;
    btnGetDeviceReady: TButton;
    btnGetPID: TButton;
    btnGetSerial: TButton;
    btnGetDataBlock: TButton;
    btnSetDataBlock: TButton;
    Label4: TLabel;
    lbVersion: TLabel;
    btnGetVersion: TButton;
    Label5: TLabel;
    lbMemorySize: TLabel;
    Button1: TButton;
    Label6: TLabel;
    edAddress: TEdit;
    memDataBlock: TMemo;
    edLength: TEdit;
    lbLength: TLabel;
    Label7: TLabel;
    edHex: TEdit;
    Label8: TLabel;
    edChar: TEdit;
    Label9: TLabel;
    edAddress2: TEdit;
    btnSetByte: TButton;
    btnGetByte: TButton;
    Label10: TLabel;
    edIteration: TEdit;
    Label11: TLabel;
    chkCharView: TCheckBox;
    Label12: TLabel;
    btnEncrypt: TButton;
    btnDecrypt: TButton;
    medString: TMaskEdit;
    lbResult: TLabel;
    PopupMenu1: TPopupMenu;
    mnuCopy: TMenuItem;
    btnCopy: TBitBtn;
    Label13: TLabel;
    Label14: TLabel;
    edHost: TEdit;
    spnPort: TSpinEdit;
    btnConnect: TButton;
    Bevel1: TBevel;
    btnDC: TButton;
    lbServerStatus: TLabel;
    Label16: TLabel;
    lbUser: TLabel;
    lbPwd: TLabel;
    edUSR: TEdit;
    edPWD: TEdit;
    lbDev: TLabel;
    cbxDevs: TComboBox;
    procedure Timer1Timer(Sender: TObject);
    procedure btnGetPIDClick(Sender: TObject);
    procedure btnGetSerialClick(Sender: TObject);
    procedure btnGetVersionClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btnGetDataBlockClick(Sender: TObject);
    procedure btnSetDataBlockClick(Sender: TObject);
    procedure edAddressKeyPress(Sender: TObject; var Key: Char);
    procedure edHexChange(Sender: TObject);
    procedure edAddress2Change(Sender: TObject);
    procedure edCharChange(Sender: TObject);
    procedure btnGetByteClick(Sender: TObject);
    procedure btnSetByteClick(Sender: TObject);
    procedure chkCharViewClick(Sender: TObject);
    procedure medStringChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure medResultChange(Sender: TObject);
    procedure btnEncryptClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
    procedure btnDecryptClick(Sender: TObject);
    procedure btnGetDeviceReadyClick(Sender: TObject);
    procedure btnConnectClick(Sender: TObject);
    procedure btnDCClick(Sender: TObject);
    procedure cbxDevsChange(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  FrmNovinDelphiSample: TFrmNovinDelphiSample;
  fStr : WideString ;
  fResult : WideString;
  clsNetC : _WSshell;
  lastCnt : integer;

implementation

{$R *.dfm}

Procedure ShowErr();
Begin
  if clsNetC.ErrNo <> 0 then
   MessageDlg('Error Number ' + inttostr(clsNetC.ErrNo)+ Char(13)+clsNetC.ErrDescr,mtError , [mbOK] ,0);
End;

procedure TFrmNovinDelphiSample.Timer1Timer(Sender: TObject);
var
  cnt: integer;
begin
    clsNetC.GetDeviceReady();
    If (clsNetC.ErrNo+1) div 2 = 15 Then //ErrNo 29 OR 30 => Loged out OR Failed
    Begin
        lbServerStatus.Caption := 'Disconnected';
        lbServerStatus.Font.Color := clRed;
        btnConnect.Enabled := true;
        btnDC.Enabled := false;
    end
    Else
    Begin
        cnt := clsNetC.GetDeviceCount;
        if cnt <> lastCnt then
        begin
          cbxDevs.Items.Clear;
          lastCnt := cnt;
          clsNetC.GetFirstDevice();
          while (cnt>0) and (clsNetC.ErrNo = 0)  do
          begin
            cbxDevs.Items.Add(clsNetC.DeviceSerial);
            if clsNetC.DeviceSerial=clsNetC.SelectedSerial then
              cbxDevs.ItemIndex := cbxDevs.Items.Count-1;
            clsNetC.GetNextDevice;
            dec(cnt);
          end;
        end;
        lbServerStatus.Caption := 'Connected' ;
        lbServerStatus.Font.Color   := clGreen ;
        btnConnect.Enabled := false;
        btnDC.Enabled := true;
    End;

end;

procedure TFrmNovinDelphiSample.btnGetPIDClick(Sender: TObject);
var
  sPWD : WideString;
begin
    sPWD:=clsNetC.ConvStringToDelimiteredString(edPassword.Text,16,'.');
    lbPID.Caption := clsNetC.GetPid(edVID.Text , sPWD);
    ShowErr();
end;

procedure TFrmNovinDelphiSample.btnGetSerialClick(Sender: TObject);
begin
    lbSerial.Caption  := clsNetC.GetSerial;
    ShowErr();
end;

procedure TFrmNovinDelphiSample.btnGetVersionClick(Sender: TObject);
begin
    lbVersion.Caption := clsNetC.GetVersion();
    ShowErr();
end;


procedure TFrmNovinDelphiSample.Button1Click(Sender: TObject);
begin
    lbMemorySize.Caption := inttostr(clsNetC.GetMemorySize);
    ShowErr();
end;

procedure TFrmNovinDelphiSample.btnGetDataBlockClick(Sender: TObject);
var
    sPWD : WideString;
begin
    Screen.Cursor :=crHourGlass ;
    sPWD:=clsNetC.ConvStringToDelimiteredString(edPassword.Text,16,'.');

    memDataBlock.Text := clsNetC.GetDataBlockStr(edVID.Text  , sPWD , strTointDef(edAddress.Text,0 ), StrToIntDef(edLength.Text  ,0));;
    Screen.Cursor := crDefault ;
    ShowErr();

end;

procedure TFrmNovinDelphiSample.btnSetDataBlockClick(Sender: TObject);
var
    P : WideString;
    sPWD : WideString;
begin
    Screen.Cursor := crHourGlass ;
    sPWD:=clsNetC.ConvStringToDelimiteredString(edPassword.Text ,16,'.');
    P := memDataBlock.Text ;
    clsNetC.SetDataBlockStr(edVID.Text  , sPWD , strTointDef(edAddress.Text,0 ), StrToIntDef(edLength.Text  ,0),P ,' ');
    Screen.Cursor := crDefault ;
    ShowErr();
end;

procedure TFrmNovinDelphiSample.edAddressKeyPress(Sender: TObject;
  var Key: Char);
begin
if not (key  in ['0','1','2','3','4','5','6','7','8','9',char(13),char(8)] ) Then Key := char(0);
end;

procedure TFrmNovinDelphiSample.edHexChange(Sender: TObject);
var
  ascii : Word;
begin
  if TEdit(Sender).Tag <> 0 Then Exit ;
  ascii := StrToIntDef(TEdit(Sender).Text , 0 );
  if ascii > 255 Then
  Begin
      TEdit(Sender).Tag := 1;
      TEdit(Sender).Text := '255';
      TEdit(Sender).Tag := 0;
      ascii := 255;
  End;
  edChar.Text := chr(ascii); 
end;

procedure TFrmNovinDelphiSample.edAddress2Change(Sender: TObject);
begin
  if TEdit(Sender).Tag <> 0 Then Exit ;
  if StrToIntDef(TEdit(Sender).Text , 0 ) > 400 Then
  Begin
      TEdit(Sender).Tag := 1;
      TEdit(Sender).Text := '400';
      TEdit(Sender).Tag := 0;
  End;


end;

procedure TFrmNovinDelphiSample.edCharChange(Sender: TObject);
begin
  try
    edHex.Text := inttoStr(ord(TEdit(Sender).text[1]));
  except;
    edhex.Text := '';
  end;

end;

procedure TFrmNovinDelphiSample.btnGetByteClick(Sender: TObject);
var
  sPWD : WideString;
begin
    sPWD:=clsNetC.ConvStringToDelimiteredString(edPassword.Text,16,'.');
    edHex.Text := inttostr(clsNetC.GetDataByte(edVID.Text , sPWD , strtointdef(edAddress2.text , 0) ));
    ShowErr();
end;

procedure TFrmNovinDelphiSample.btnSetByteClick(Sender: TObject);
var
  Databyte : byte;
  sPWD : WideString;
begin
    DataByte := StrTointDef(edHex.Text,0)  ;
    sPWD := clsNetC.ConvStringToDelimiteredString(edPassword.Text,16,'.');
    clsNetC.SetDataByte(edVID.Text , sPWD , strtointdef(edAddress2.text , 0) , Databyte );
    ShowErr();
end;

procedure TFrmNovinDelphiSample.chkCharViewClick(Sender: TObject);
var
  TempS : WideString;
begin
    Try
        medString.Tag := 1;
        lbResult.Tag := 1;

        if Not chkCharView.Checked Then
        Begin
            medString.Text := '';
            lbResult.Caption := '';

            medString.EditMask := '999.999.999.999.999.999.999.999.999.999.999.999.999.999.999.999;1; ';


            medString.Text := fStr ;
            lbResult.Caption := fResult ;
        END
        else
        BEGIN
            medString.EditMask := '';
            medString.Text := '';
            medString.MaxLength := 16;
            TempS :=clsNetC.ConvDelimiteredStringToString(fStr,16,'.');
            medString.Text := Temps;
            TempS := '';

            TempS:=clsNetC.ConvDelimiteredStringToString(fResult  , 16,'.');
            lbResult.Caption := TempS;
        END;
    Finally
        medString.Tag := 0 ;
    end;

end;

procedure TFrmNovinDelphiSample.medStringChange(Sender: TObject);
var
  TempS : WideString;
  s1 : WideSTring;
begin
    if medString.Tag = 0 Then
    Begin
        s1 := medString.Text;
        if chkCharView.Checked Then
        Begin
             TempS := clsNetC.ConvStringToDelimiteredString(S1 , 16,'.');
             fStr := Temps;
        end
        else
        Begin
            S1 := StringReplace(S1 , ' ', '' , [rfReplaceAll ]);
            fstr := S1;
        end;
    end;
end;

procedure TFrmNovinDelphiSample.FormCreate(Sender: TObject);
begin
  clsNetC := CoWSshell.Create;
  clsNetC.init;
  lastCnt := 0;
  fstr := '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0';
  fResult := '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0';
end;

procedure TFrmNovinDelphiSample.medResultChange(Sender: TObject);
var
  TempS : WideString;
  s1 : WideSTring;
begin
    if lbResult.Tag = 0 Then
    Begin
        s1 := lbResult.Caption ;
        if chkCharView.Checked Then
        Begin
             TempS := clsNetC.ConvStringToDelimiteredString(S1 , 16,'.');
             fResult := Temps;
        end
        else
        Begin
            S1 := StringReplace(S1 , ' ', '' , [rfReplaceAll ]);
            fResult  := S1;
        end;
    end;
end;

procedure TFrmNovinDelphiSample.btnEncryptClick(Sender: TObject);
Var
  sPWD : WideString;
begin
    fREsult := '';
    sPWD:=clsNetC.ConvStringToDelimiteredString(edPassword.Text , 16,'.');
    fResult := clsNetC.GetEncryption(edVID.Text , sPWD , fStr , strTointDef(edIteration.Text , 0) );
    chkCharViewClick(chkCharView );

end;

procedure TFrmNovinDelphiSample.btnCopyClick(Sender: TObject);
begin
  fStr := fResult ;
  chkCharViewClick(chkCharView );
end;

procedure TFrmNovinDelphiSample.btnDecryptClick(Sender: TObject);
Var
  sPWD : WideString;
begin
    fREsult := '';
    sPWD:= clsNetC.ConvStringToDelimiteredString(edPassword.Text,16,'.');
    fResult :=clsNetC.GetDecryption(edVID.Text , sPWD , fStr , strTointDef(edIteration.Text , 0) );
    chkCharViewClick(chkCharView );

end;

procedure TFrmNovinDelphiSample.btnGetDeviceReadyClick(Sender: TObject);
begin
  clsNetC.GetDeviceReady;
  lbChkDeviceReady.Caption := clsNetC.ErrDescr;
end;

procedure TFrmNovinDelphiSample.btnConnectClick(Sender: TObject);
begin
  Timer1.Enabled := false;
  clsNetC.Host:= edHost.Text;
  clsNetC.Port := spnPort.Value;
  clsNetC.Login(edUSR.Text,edPWD.Text);
  if clsNetC.ErrNo <> 0 then
    ShowErr()
  else
    Timer1.Enabled := true;
end;

procedure TFrmNovinDelphiSample.btnDCClick(Sender: TObject);
begin
  clsNetC.Logout;
  if clsNetC.ErrNo  = 0 then
  begin
    Timer1.Enabled := false;
    btnConnect.Enabled := true;
    btnDC.Enabled := false;
    lbServerStatus.Caption := 'Disconnected';
  end;
end;

procedure TFrmNovinDelphiSample.cbxDevsChange(Sender: TObject);
begin
  clsNetC.SelectDevice(cbxDevs.Items[cbxDevs.itemindex]);
  clsNetC.GetDeviceReady();
  If clsNetC.ErrNo = 0 Then
  Begin
      lbChkDeviceReady.Caption := 'Ready' ;
      lbChkDeviceReady.Font.Color   := clGreen ;
  end
  Else
  Begin
      lbChkDeviceReady.Caption := 'Not Ready';
      lbChkDeviceReady.Font.Color := clRed;
  End;
end;

end.
